
package edu.chalmers.AV_2DPlatformerFramework.Model;

import edu.chalmers.AV_2DPlatformerFramework.Utility.Utility;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * Provides addition functionality to TexturePaint related to the framework.
 * @version 0.1
 * @author Richard
 */
public class Texture extends TexturePaint{
    
    public Texture(String imageURL, Shape anchor){
        super(Utility.generateBufferedImage(imageURL), anchor.getBounds2D());
    }
    public Texture(BufferedImage bImg, Shape anchor){
        super(bImg, anchor.getBounds2D());
    }
    
    /**
     * Returns a texture based on this GameObject's current location.
     * @param newAnchor The new anchor to use.
     * @return The Texture with translation applied.
     */
    public Texture getUpdatedTexture(Rectangle2D newAnchor){
          return new Texture(this.getImage(), newAnchor);
    }
    
    /**
     * Returns a texture based on this GameObject's current location.
     * @param go The object to anchor this texture to.
     * @return The Texture with translation applied.
     */    
    public Texture getUpdatedTexture(GameObject go){
          return new Texture(this.getImage(), go.getHitbox().getBounds2D());
    }
    
    /**
     * Draws this texture using the supplied Graphics2D object.
     * @param g2d The graphics to draw on.
     * @param drawingArea The gameObject whose BOUNDING BOX is used as the anchor.
     * @param restore Will reset any changes made to the Graphics2D object before returning if true.
     */
    public void drawMe(Graphics2D g2d, Rectangle2D drawingArea, boolean restore){
        Paint backup = null;
        if (restore)
            backup = g2d.getPaint();
        
        g2d.setPaint(this.getUpdatedTexture(drawingArea));
        g2d.fill(drawingArea);
        
        if (restore)
            g2d.setPaint(backup);
        
    }
     /**
     * Draws this texture using the supplied Graphics2D object.
     * WARNING: The state of the Graphics2D may change!
     * @param g2d The graphics to draw on.
     * @param drawingArea The gameObject whose BOUNDING BOX is used as the anchor.
     */
    public void drawMe(Graphics2D g2d, Rectangle2D drawingArea){
        drawMe(g2d, drawingArea, false);
    }
    
    public Texture copy(){
        return new Texture(this.getImage(), this.getAnchorRect());
    }
    
}
